package com.gimi.cloud.jimi.manage.service.userInfo.Impl;


import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.dao.dao.User.UserDao;
import com.gimi.cloud.jimi.dao.dao.User.UserInfoDao;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gimi.cloud.jimi.dto.userInfo.RdUserInfoDTO;
import com.gimi.cloud.jimi.entity.userInfo.RdUserInfoEntity;
import com.gimi.cloud.jimi.manage.service.userInfo.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
@Transactional
public class UserInfoServiceImpl implements UserInfoService {
	
	@Autowired
	private UserInfoDao userInfoDao;
	@Autowired
	private UserDao userDao;


	@Override
	public List<RdUserInfoEntity> getUsreInfoList() {
		List<RdUserInfoEntity> list = userInfoDao.selectAll();
		return list;
	}

	@Override
	public PageInfo<RdUserInfoDTO> getUserInfoPageList(RdUserInfoDTO userInfoDTO) {
	    PageHelper.startPage(userInfoDTO.getCurrentPage(), userInfoDTO.getPageSize());
	    List<RdUserInfoDTO> list = userInfoDao.selectAllAndStatus();
	    if(list != null) {
	    	for(RdUserInfoDTO dto : list) {
				dto.setCardHand(AliOSSUtil.getFileUrl(dto.getCardHand(), AliOSSUtil.getNoEffictDateByDay(1)));
				dto.setCardBack(AliOSSUtil.getFileUrl(dto.getCardBack(), AliOSSUtil.getNoEffictDateByDay(1)));
				dto.setCardFront(AliOSSUtil.getFileUrl(dto.getCardFront(), AliOSSUtil.getNoEffictDateByDay(1)));
				dto.setPhoto(AliOSSUtil.getFileUrl(dto.getPhoto(), AliOSSUtil.getNoEffictDateByDay(1)));
			}
		}
	    PageInfo<RdUserInfoDTO> page = new PageInfo<>(list);
		return page;
	}

	@Override
	public Long getCount() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public RdUserInfoDTO getUserInfoSingle(RdUserInfoDTO userInfoDTO) {
		RdUserInfoEntity userInfoEntity = userInfoDao.selectByUserId(userInfoDTO.getUserId());
		RdUserInfoDTO dto = new RdUserInfoDTO();
		BeanUtils.copyProperties(userInfoEntity, dto);
		Date imgDate = new Date();
		dto.setCardHand(AliOSSUtil.getFileUrl(dto.getCardHand(), AliOSSUtil.getNoEffictDateByDay(1)));
		dto.setCardBack(AliOSSUtil.getFileUrl(dto.getCardBack(), AliOSSUtil.getNoEffictDateByDay(1)));
		dto.setCardFront(AliOSSUtil.getFileUrl(dto.getCardFront(), AliOSSUtil.getNoEffictDateByDay(1)));
		dto.setPhoto(AliOSSUtil.getFileUrl(dto.getPhoto(), AliOSSUtil.getNoEffictDateByDay(1)));
		return dto;
	}

	@Override
	public void updateUserInfo(RdUserInfoDTO userInfoDTO) {
		userInfoDTO.setPhoto(AliOSSUtil.getFilePath(userInfoDTO.getPhoto()));
		userInfoDTO.setCardFront(AliOSSUtil.getFilePath(userInfoDTO.getCardFront()));
		userInfoDTO.setCardBack(AliOSSUtil.getFilePath(userInfoDTO.getCardBack()));
		userInfoDTO.setCardHand(AliOSSUtil.getFilePath(userInfoDTO.getCardHand()));
		RdUserInfoEntity record = new RdUserInfoEntity();
		BeanUtils.copyProperties(userInfoDTO, record);
		int i = userInfoDao.updateByPrimaryKeySelective(record);
		if (i <= 0) {
			throw new MsgException(ResponseCodeEnum.INVALID);
		}
	}

	@Override
	public void userIsCanBeUsed(RdUserInfoDTO userInfoDTO) {
		userDao.userIsCanBeUsed(userInfoDTO);
	}

}
